package com.zfk.transaction.service.impl;

import com.zfk.transaction.entity.Seller;
import com.zfk.transaction.entity.User;

import com.zfk.transaction.mapper.UserMapper;
import com.zfk.transaction.service.UserService;
import com.zfk.transaction.util.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author : zfk
 * Date : 10:38
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String email, String password) {
        // 密码加密
        password = Base64Utils.encode(password);
         return userMapper.getByEmailAndPassword(email,password);
    }

    @Override
    public Integer add(User user) {
        // 密码加密
        user.setPassword(Base64Utils.encode(user.getPassword()));
        // 先判断该email是否以注册
        User userByEmail = userMapper.getByEmail(user.getEmail());
        System.out.println("service :"+user);
        System.out.println("service by email ="+userByEmail);
        // 查询到为null ，即可以注册
        if (userByEmail == null || userByEmail.getEmail() == null){
            return userMapper.add(user);
        }else
            return 0;
    }

    @Override
    public User getByEmail(String email) {
        return userMapper.getByEmail(email);
    }

    @Override
    public int updateByEmail(String email, String password) {

        password = Base64Utils.encode(password);

        return userMapper.updateByEmail(email,password);
    }

    @Override
    public Seller getById(int id) {
        User user = userMapper.getById(id);
        Seller seller = new Seller(user.getId(),user.getUserName());

        return seller;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }
}
